package com.nari.flutter_desbase64

import android.text.TextUtils
import android.os.Build
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import java.text.SimpleDateFormat
import java.util.*

/**
 * FlutterDesbase64Plugin
 */
class FlutterDesbase64Plugin : FlutterPlugin, MethodCallHandler {
    /// The MethodChannel that will the communication between Flutter and native
    /// Android
    ///
    /// This local reference serves to register the plugin with the Flutter Engine
    /// and unregister it
    /// when the Flutter Engine is detached from the Activity
    private var channel: MethodChannel? = null
    override fun onAttachedToEngine(flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
        channel = MethodChannel(flutterPluginBinding.getBinaryMessenger(), "flutter_desbase64")
        channel?.setMethodCallHandler(this)
    }

    override fun onMethodCall(call: MethodCall, result: Result) {
        val callMethod: String = call.method
        if (!TextUtils.isEmpty(callMethod)) {
            when (callMethod) {
                "getPlatformVersion" -> result.success("Android " + Build.VERSION.RELEASE)
                "jyZipAndDecodeInfo" -> {
                    val originString: String = call.argument<String>("originString") as String
                    val decodeString = DESBase64Util.jyZipAndDecodeInfo(originString)
                    result.success(decodeString)
                }
                "getBuildTime" -> {
                    val format = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                    val date: Date = BuildConfig.BUILD_TIME
                    val buildTime = format.format(date)
                    result.success(buildTime)
                }
                else -> result.notImplemented()
            }
        } else {
            result.notImplemented()
        }
    }

    override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
        channel?.setMethodCallHandler(null)
    }
}